/** 
 * @Title:单独开启一线程用于监听节点leader选举 
 * @Desription:单独开启一线程用于监听节点leader选举
 * @Company:CSN
 * @ClassName:LeaderThread.java
 * @Author:Justic
 * @CreateDate:2015-6-30   
 * @UpdateUser:Justic  
 * @Version:0.1 
 *    
 */

package com.justic.zk.quartz.curator.support;

import org.apache.curator.framework.recipes.leader.LeaderSelector;
import org.apache.curator.utils.EnsurePath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.justic.zk.quartz.curator.ICuratorWrapper;

/**
 * @ClassName: LeaderThread
 * @Description: 单独开启一线程用于监听节点leader选举
 * @author: Justic
 * @date: 2015-6-30
 * 
 */
public class LeaderSelectorThread implements Runnable {

	private Logger logger = LoggerFactory.getLogger(LeaderSelectorThread.class);

	private ICuratorWrapper curator;

	/**
	 * @param curator
	 */
	public LeaderSelectorThread(ICuratorWrapper curator) {
		this.curator = curator;
	}

	/**
	 * 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		try {
			// ensure path of /csair/zk/quartz
			new EnsurePath(curator.getRootPath()).ensure(curator.getClient().getZookeeperClient());

			LeaderSelector leaderSelector = new LeaderSelector(curator.getClient(), curator.getRootPath(),
					new LeaderListener(curator));
			
			leaderSelector.start();
			

		} catch (Exception e) {
			logger.error("LeaderSelectorThread runing exception====>>{}", e);
		}

	}

}
